/**
 * Created by Admin on 2017/8/18.
 */
var showFPS = (function(){
    var requestAnimationFrame =
        window.requestAnimationFrame || //Chromium
        window.webkitRequestAnimationFrame || //Webkit
        window.mozRequestAnimationFrame || //Mozilla Geko
        window.oRequestAnimationFrame || //Opera Presto
        window.msRequestAnimationFrame || //IE Trident?
        function(callback) { //Fallback function
            window.setTimeout(callback, 1000/60);
        };
    var e,pe,pid,fps,last,offset,step,appendFps;

    fps = 0;
    last = Date.now();
    step = function(){
        offset = Date.now() - last;
        fps += 1;
        if( offset >= 1000 ){
            last += offset;
            appendFps(fps);
            fps = 0;
        }
        requestAnimationFrame( step );
    };
    //显示fps; 如果未指定元素id，默认<body>标签
    appendFps = function(fps){
        pe=pid?document.getElementById(pid):document.getElementsByTagName('body')[0];
        if(!e){
            e=document.createElement('span');
            e.className = "fps";
            pe.appendChild(e);
        }
        e.innerText = "fps: " + fps;
    }
    return {
        setParentElementId :  function(id){pid=id;},
        go                 :  function(){step();}
    }
})();
showFPS.setParentElementId();
showFPS.go();